use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .create_table(
                Table::create()
                    .table(Customers::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(Customers::Id)
                            .string()
                            .not_null()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(Customers::Email).string().not_null())
                    .col(ColumnDef::new(Customers::Password).string().not_null())
                    .col(ColumnDef::new(Customers::Name).string().not_null())
                    .col(ColumnDef::new(Customers::Avatar).string().not_null())
                    .col(
                        ColumnDef::new(Customers::CustomerGroupId)
                            .string()
                            .not_null(),
                    )
                    .col(ColumnDef::new(Customers::AddressId).string().not_null())
                    .col(ColumnDef::new(Customers::Locale).string().not_null())
                    .col(
                        ColumnDef::new(Customers::Active)
                            .boolean()
                            .not_null()
                            .default(true),
                    )
                    .col(ColumnDef::new(Customers::Code).string().not_null())
                    .col(ColumnDef::new(Customers::From).string().not_null())
                    .col(
                        ColumnDef::new(Customers::DeletedAt)
                            .timestamp_with_time_zone()
                            .null(),
                    )
                    .col(
                        ColumnDef::new(Customers::CreatedAt)
                            .timestamp_with_time_zone()
                            .default(Expr::current_timestamp()),
                    )
                    .col(
                        ColumnDef::new(Customers::UpdatedAt)
                            .timestamp_with_time_zone()
                            .default(Expr::current_timestamp()),
                    )
                    .index(
                        Index::create()
                            .name("idx_customers_email_unique")
                            .col(Customers::Email)
                            .unique(),
                    )
                    .foreign_key(
                        ForeignKey::create()
                            .name("fk_customers_customer_group_id")
                            .from(Customers::Table, Customers::CustomerGroupId)
                            .to(CustomerGroups::Table, CustomerGroups::Id)
                            .on_delete(ForeignKeyAction::Cascade)
                            .on_update(ForeignKeyAction::Cascade),
                    )
                    .foreign_key(
                        ForeignKey::create()
                            .name("fk_customers_address_id")
                            .from(Customers::Table, Customers::AddressId)
                            .to(Addresses::Table, Addresses::Id)
                            .on_delete(ForeignKeyAction::Cascade)
                            .on_update(ForeignKeyAction::Cascade),
                    )
                    .to_owned(),
            )
            .await
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table(Customers::Table).to_owned())
            .await
    }
}

#[derive(DeriveIden)]
enum Customers {
    Table,
    Id,
    Email,
    Password,
    Name,
    Avatar,
    CustomerGroupId,
    AddressId,
    Locale,
    Active,
    Code,
    From,
    DeletedAt,
    CreatedAt,
    UpdatedAt,
}

#[derive(DeriveIden)]
enum CustomerGroups {
    Table,
    Id,
}

#[derive(DeriveIden)]
enum Addresses {
    Table,
    Id,
}
